package com.ebook.ui.fragment.home.shuffle;

import com.ebook.ui.activity.HomeActivity;
import com.ebook.ui.fragment.Fragment;
import com.ebook.ui.sync.WorkSyncer;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

/**
 * 该Fragment主要处理的是轮播图逻辑.图片是由背景(paintComponent中绘制).两个按钮添加了合成绘制在该Fragment的JPanel上面.
 * 预期修正:丰富ShufflingItem使得图片可以<strong>追踪<strong/>到图书.并且ShufflingItem信息改为冲数据库或数据源中随机获取.
 * **/
public class ShufflingFragment extends Fragment {
    private JLabel next;
    private JLabel pre;

    private ArrayList<ShufflingItem> shuffleItems;
    private volatile boolean go = true;
    private int point = 0;
    private ImageIcon view;
    private Thread shuffleThread;
    private Thread sleepThread;
    private volatile int clock;
    public ShufflingFragment(){
        super();
        super.serializeId = 11003;
        initComponents();
        this.repaint();
    }
    @Override
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        g.drawImage(view.getImage(),0,0,getWidth(),getHeight(),this);
    }
    @Override
    public void initComponents(){
        this.setBounds(364,90,696,390);
        ArrayList<ShufflingItem> items = new ArrayList<>();
        items.add(new ShufflingItem(10,"G:\\Java\\ebook_maven\\src\\main\\resources\\logic\\ui\\shuffle\\1.png"));
        items.add(new ShufflingItem(11,"G:\\Java\\ebook_maven\\src\\main\\resources\\logic\\ui\\shuffle\\2.jpg"));
        items.add(new ShufflingItem(12,"G:\\Java\\ebook_maven\\src\\main\\resources\\logic\\ui\\shuffle\\3.jpg"));
        shufflingComponent(items);
        pre = new TranslucentLabel("L");
        pre.setBounds(0,160,50,40);
        next = new TranslucentLabel("R");
        next.setBounds(646,160,50,40);
        pre.addMouseListener(new MouseAdapter(){
            @Override
            public void mouseClicked(MouseEvent e){
                turnToPre();
                sleep();
//                System.out.println(shufflingComponent.getClock()+"clicked");
            }
        });
        next.addMouseListener(new MouseAdapter(){
            @Override
            public void mouseClicked(MouseEvent e){
                turnToNext();
                sleep();
//                System.out.println(shufflingComponent.getClock()+"clicked");
            }
        });
        this.add(pre);
        this.add(next);
    }
    public void shufflingComponent(ArrayList<ShufflingItem> shuffleItems){
        this.shuffleItems = shuffleItems;
        view = new ImageIcon
                (shuffleItems.get(point).getImg_path());
        shuffleThread = getShuffleThread();
        shuffleThread.start();
        Thread t1 = new Thread(()->{
            while(true){
                try{
                    Thread.sleep(100);
                    clockA();
//                    System.out.println(getClock()+" go"+go);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        t1.start();
    }
    public synchronized void clockA(){
        clock = clock + 1;
    }
    public synchronized int getClock(){
        return clock;
    }
    public void turnToPre(){
        point = point - 1;
        if(point<0){
            point = this.shuffleItems.size()-1;
        }
        view = new ImageIcon(shuffleItems.get(point).getImg_path());
        this.repaint();
    }
    public void turnToNext(){
        point = point + 1;
        if(point>=this.shuffleItems.size()){
            point = 0;
        }
        view = new ImageIcon(shuffleItems.get(point).getImg_path());
        this.repaint();
    }
    public void sleep(){
        go = false;
        if(sleepThread!=null){
            sleepThread.interrupt();
        }
        sleepThread = new Thread(new Runnable(){
            @Override
            public void run() {
                try {
                    Thread.sleep(3000);
                    go = true;
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        sleepThread.start();
    }
    public Thread getShuffleThread(){
        return new Thread(new Runnable(){
            @Override
            public void run() {
                while(true){
                    while(go){
                        try{
//                            System.out.println(getClock()+"go=="+go);
                            if(WorkSyncer.webView==null){
                                Thread.sleep(2000);
                            }else if(WorkSyncer.webView.peekActivity() instanceof HomeActivity){
                                SwingUtilities.invokeLater(new Runnable(){
                                    @Override
                                    public void run() {
                                        ShufflingFragment.this.turnToNext();
                                    }
                                });
                                Thread.sleep(2000);
//                            System.out.println("在EDT中运行"+SwingUtilities.isEventDispatchThread());
                            }

                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        });
    }
}
